package com.dragon.www.smarthome.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.entity.Device;
import com.dragon.www.smarthome.entity.DeviceInfo;
import com.dragon.www.smarthome.entity.SingleReturnInfo;
import com.dragon.www.smarthome.net.NetHelper;
import com.dragon.www.smarthome.net.NetUtils;
import com.dragon.www.smarthome.view.BaseItemView;

/**
 * Created by liqp on 2016/3/16.
 */
public class HumidifierAdapter extends AppArrayAdapter<Device, HumidifierAdapter.HumidifierViewHolder> {

    public HumidifierAdapter(Context context){
        super(context, R.layout.humidifier_view_item, HumidifierViewHolder.class);
    }

    public View bindHolder(View convertView, ViewGroup parent, int position) {
        final Device humidifier = this.devices.get(position);
        HumidifierViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new HumidifierViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.humidifier_view_item, parent, false);
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 2) - 1, (int) (parent.getHeight() / 3)));// 动态设置item的高度
            viewHolder.tv_humidifier_name = (TextView) convertView.findViewById(R.id.tv_humidifier_desc);
            viewHolder.tv_humidifier_status = (BaseItemView) convertView.findViewById(R.id.baseItemView_humidifier);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (HumidifierViewHolder) convertView.getTag();
            convertView.setLayoutParams(new AbsListView.LayoutParams((int) (parent.getWidth() / 2) - 1, (int) (parent.getHeight() / 3)));// 动态设置item的高度
        }

        viewHolder.tv_humidifier_name.setText(humidifier.getDeviceName());
        viewHolder.tv_humidifier_status.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (deviceInfo != null ){
                        if ( Integer.parseInt(humidifier.getequipNo().split("-")[0]) == box1){
                            if (deviceInfo[0].getRelayValues()[Integer.parseInt(humidifier.getequipNo().split("-")[1])-1] == 0) {
                                setAction(humidifier, 1,box1);
                            }else if(deviceInfo[0].getRelayValues()[Integer.parseInt(humidifier.getequipNo().split("-")[1])-1] == 1) {
                                setAction(humidifier, 0,box1);
                            }
                        }else if ( Integer.parseInt(humidifier.getequipNo().split("-")[0]) == box2){
                            if (deviceInfo[1].getRelayValues()[Integer.parseInt(humidifier.getequipNo().split("-")[1])-1] == 0) {
                                setAction(humidifier, 1,box2);
                            }else if(deviceInfo[1].getRelayValues()[Integer.parseInt(humidifier.getequipNo().split("-")[1])-1] == 1) {
                                setAction(humidifier, 0,box2);
                            }
                        }
                    }
                }
                return false;
            }
        });
        if (deviceInfo != null ){
            if ( Integer.parseInt(humidifier.getequipNo().split("-")[0]) == box1) {
                if (deviceInfo[0].getRelayValues()[Integer.parseInt(humidifier.getequipNo().split("-")[1]) - 1] == 0) {

                    viewHolder.tv_humidifier_status.turnOff();

                } else if (deviceInfo[0].getRelayValues()[Integer.parseInt(humidifier.getequipNo().split("-")[1]) - 1] == 1) {
                    viewHolder.tv_humidifier_status.turnOn();

                }
            }else if( Integer.parseInt(humidifier.getequipNo().split("-")[0]) == box2) {
                if (deviceInfo[1].getRelayValues()[Integer.parseInt(humidifier.getequipNo().split("-")[1]) - 1] == 0) {
                    viewHolder.tv_humidifier_status.turnOff();
                } else if (deviceInfo[1].getRelayValues()[Integer.parseInt(humidifier.getequipNo().split("-")[1]) - 1] == 1) {
                    viewHolder.tv_humidifier_status.turnOn();
                }
            }
        }
        return convertView;
    }

    public void setAction (Device humidifier, int action,int boxid){
        NetHelper.sendAction(context, boxid, 1, Integer.parseInt(humidifier.getequipNo().split("-")[1]), "power", action, "relay", new NetUtils.NetCallBack<DeviceInfo>() {

            @Override
            public void success(DeviceInfo rspData) {
                if (rspData.getError() == 0) {
//                    final SingleReturnInfo SingleReturnInfo = (SingleReturnInfo) rspData;
//                    int value = SingleReturnInfo.getReturnInfo().getValue();
//                    deviceInfo[0].getRelayValues()[Integer.parseInt(lamp.getequipNo().split("-")[1])-1] = value;
//                    notifyDataSetChanged();
                } else {
                    String description = rspData.getDescription();
                    Log.e("error", description);
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        }, SingleReturnInfo.class);

    }

    public class HumidifierViewHolder extends AppHolder {
        TextView tv_humidifier_name;
        BaseItemView tv_humidifier_status;
    }
}

